package manage.action;

import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.system.util.ArrayUtil;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.AdminGroupLink;

import java.util.List;

@ActionMeta(name="manage/pageUtil")
public class PageUtilAction extends ManageAction {

	@RequestMeta(params= {"orgGroup","roleGroup"})
	public JSONMessage getAdminLink(String orgGroup,String roleGroup) throws Exception {
		verifyAdminLogin();
		ModelQueryList inQueryList;
		if(!StringUtil.isSpace(orgGroup)) {
			String[] nodes=ArrayUtil.toNoEmptyArray(StringUtil.noSpace(orgGroup).split(","));
			inQueryList=ModelQueryList.instance(AdminGroupLink.class, new String[] {"admin.oid"}, null,
				QueryCondition.and(
					QueryCondition.eq("adminGroup.type", "C"),
					QueryCondition.or(
						QueryCondition.in("adminGroup.oid", nodes),
						QueryCondition.in("adminGroup.num", nodes),
						QueryCondition.in("adminGroup.parent.oid", nodes),
						QueryCondition.in("adminGroup.parent.num", nodes),
						QueryCondition.in("adminGroup.org.oid", nodes),
						QueryCondition.in("adminGroup.org.num", nodes)
					)
				)
			);
		}else {
			inQueryList=ModelQueryList.instance(AdminGroupLink.class, new String[] {"admin.oid"}, null,
				QueryCondition.eq("adminGroup.type", "C")
			);
		}
		if(!StringUtil.isSpace(roleGroup)) {
			String[] flter=ArrayUtil.toNoEmptyArray(StringUtil.noSpace(roleGroup).split(","));
			inQueryList=ModelQueryList.instance(AdminGroupLink.class, new String[] {"admin.oid"}, null,
				QueryCondition.and(
					QueryCondition.eq("adminGroup.type", "B"),
					QueryCondition.or(
						QueryCondition.in("adminGroup.oid", flter),
						QueryCondition.in("adminGroup.num", flter)
					),
					QueryCondition.in("admin.oid", inQueryList)
				)
			);
//			}else {
//				inQueryList=ModelQueryList.instance(AdminGroupLink.class, new String[] {"admin.oid"}, null,
//					QueryCondition.and(
//						QueryCondition.eq("adminGroup.type", "B"),
//						QueryCondition.in("admin.oid", inQueryList)
//					)
//				);
		}
		List<AdminGroupLink> list=ModelQueryList.getModelList(AdminGroupLink.class,
			new String[] {"*","admin.*","adminGroup.*","adminGroup.parent.*"},getPage(),
			QueryCondition.and(
				QueryCondition.eq("admin.status", "0"),
				QueryCondition.in("admin.oid",inQueryList)
			),
			QueryOrder.asc("admin.oid")
		);
		return new JSONMessage(0)
				.push("list", list);
	}

}
